#!/bin/bash
#********************************************************************
# Author: zhangjunjie
# Date: 2022-06-26
# FileName：ubuntu_install_nginx.sh
#********************************************************************

### ----------- 源码安装常量的位置 ----------- ###
SRC_DIR='/usr/local/src'
PREFIX='/apps/nginx'
NGINX_VERSION="nginx-1.18.0"
NGINX_FILE="${NGINX_VERSION}.tar.gz"
NGINX_DOWN_URL="https://nginx.org/download/${NGINX_FILE}"
### ----------- 源码安装常量的位置 ----------- ###

## 首先更新 yum 源信息
apt update
apt -y install wget

## 先判断是否存在自定义的方法文件, 如果不存在, 则进行下载操作
if [ ! -e self_function.sh ]; then
	echo -e "\E[33;1m 下载自定义方法中 \E[0m"
	wget https://gitee.com/gkdaxue/shell_script/raw/master/self_function.sh
	if [ ! -e self_function.sh ]; then 
		echo -e "\E[31;1m 下载自定义方法失败 \E[0m"
		exit;
	fi
fi

## 加载自定义方法
. ./self_function.sh
actions 'echo_tips' "加载自定义方法";

menus=(
"source_code"
)
PS3="请选择你要如何安装 : "
echo "我们提供如下菜单: "
select menu in ${menus[@]} ; do
	case $menu in
		## 源码安装	
		"source_code")
			apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
			
			actions 'echo_tips' "开始进行源码安装了"
			id nginx || { actions 'echo_tips' "创建 nginx 用户";  useradd -s /sbin/nologin -r  nginx; }
			mkdir -p ${PREFIX}/run
			cd $SRC_DIR

			## 如果没有安装包, 则进行下载操作
			if [ ! -e $NGINX_FILE ]; then
				wget $NGINX_DOWN_URL
			fi
				
			tar xvf "${NGINX_VERSION}.tar.gz"
			cd $NGINX_VERSION
			actions 'echo_tips' "开始进行编译了"
			./configure --prefix=${PREFIX} \
			--user=nginx \
			--group=nginx \
			--with-http_ssl_module \
			--with-http_v2_module \
			--with-http_realip_module \
			--with-http_stub_status_module \
			--with-http_gzip_static_module \
			--with-pcre \
			--with-stream \
			--with-stream_ssl_module \
			--with-stream_realip_module
			make && make install
			chown -Rf nginx.nginx ${PREFIX}

			sed -i.bak -e "/#pid /c pid ${PREFIX}/run/nginx.pid;" ${PREFIX}/conf/nginx.conf
			actions 'echo_tips' "开始创建 Service 文件"
			echo "PATH=${PREFIX}/sbin:\$PATH" > /etc/profile.d/nginx.sh
			cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=${PREFIX}/run/nginx.pid
ExecStartPre=/bin/rm -f ${PREFIX}/run/nginx.pid
ExecStartPre=${PREFIX}/sbin/nginx -t
ExecStart=${PREFIX}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
			
			## 创建日志切割功能
			cat > /etc/logrotate.d/nginx <<EOF
/var/log/nginx/*.log {
		daily
		missingok
		rotate 52
		compress
		delaycompress
		notifempty
		create 640 nginx adm
		sharedscripts
		postrotate
				if [ -f ${PREFIX}/run/nginx.pid ]; then
						kill -USR1 \`cat ${PREFIX}/run/nginx.pid\`
				fi
		endscript
}
EOF

			systemctl daemon-reload
			systemctl start nginx
			systemctl status nginx
			exit;
			;;			
		*)
			echo "Selected number: $REPLY"
			echo "Selected number: $menu"
			exit;
			;;
	esac
done


